package com.zx.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zx.DAO.UserDao;
import com.zx.entity.Ur;
import com.zx.entity.User;
import com.zx.service.UserInte;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 程仟磊
 * @date 2021/7/23
 */
@Service
public class UserImpl implements UserInte {
    @Resource
    private UserDao userDao;
    private static final int PAGESIZE=2;
    private int pages;

    @Override
    public int addUser(String uname, String uaccount, String upsw) {
        User user=new User();
        user.setUsaccount(uaccount);
        user.setUsname(uname);
        user.setUspsw(upsw);
        return userDao.register(user);
    }

    @Override
    public int updateUserByAccount(String uid,String uname,String upsw,String uphone) {
        User user=new User();
        user.setUsid(Integer.parseInt(uid));
        user.setUsname(uname);
        user.setUspsw(upsw);
        user.setUsphone(uphone);
        return userDao.updateUserById(user);
    }

    @Override
    public List<User> findAllUser(String page) {
        // page 第几页，pagesize每页有几条数据
        Page<Object> objects = PageHelper.startPage(Integer.parseInt(page), PAGESIZE);
        List<User> allUser = userDao.findAllUser();
        //获取总页数
        pages= objects.getPages();
        return allUser;
    }

    @Override
    public int getPages() {
        return pages;
    }

    @Override
    public void addRoleToUser(String uid,String[] rid) {
        for (int i = 0; i < rid.length; i++) {
            userDao.addRoleToUser(Integer.parseInt(uid),Integer.parseInt(rid[i]));
        }
    }

    @Override
    public void deleteUser(String uid) {
        int i = userDao.deleteUr(Integer.parseInt(uid));
        if (i>0){
        userDao.deleteUser(Integer.parseInt(uid));}
    }


}
